//
//  ClearDataViewController.m
//  XDLBaseProject
//
//  Created by 谢东来 on 2019/9/10.
//  Copyright © 2019 Apple. All rights reserved.
//

#import "ClearDataViewController.h"
#import "BaseTableViewCell.h"

@interface ClearDataViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;

@end

@implementation ClearDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"数据管理";
    
    [self.view addSubview:self.tableView];
    [self.tableView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
}

#pragma mark UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BaseTableViewCell *cell = [BaseTableViewCell cellWithTableView:self.tableView indentifier:@"cell" style:(UITableViewCellStyleValue1)] ;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UILabel *lab = [UILabel newAutoLayoutView];
    lab.text = @"重置所有数据";
    lab.textColor = UIColorFromHex(0xc00000);
    lab.font = [AppFont getFont_15];
    [cell addSubview:lab];
    [lab autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
    [lab autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WeakSelf(weakSelf)
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否重置所有数据？" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        [SVProgressHUD show];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD showSuccessWithStatus:@"數據已清空"];
        });
//        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//        NSDictionary *parameters = @{@"zhanghao":[DataManager shareManager].dataModel.zhanghao};
//        [manager POST:API_CLEARDATA parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
//        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            NSLog(@"%@", responseObject);
//            if ([responseObject[@"status"] integerValue] == 1) {
//                [SVProgressHUD showSuccessWithStatus:@"數據已清空"];
//                [[DataManager shareManager] clearTempDataWithID:[DataManager shareManager].dataModel.zhanghao];
//                [DataManager shareManager].dataModel.baseMoney = 0;
//                [[DataManager shareManager] saveData];
//                RegisterViewController *v = [[RegisterViewController alloc] init];
//                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:v];
//                nav.modalPresentationStyle = UIModalPresentationFullScreen;
//                [weakSelf.navigationController presentViewController:nav animated:YES completion:^{
//                    weakSelf.tabBarController.selectedIndex = 0;
//                    [weakSelf.navigationController popViewControllerAnimated:NO];
//                }];
//
//            } else {
//                [SVProgressHUD showSuccessWithStatus:@"數據清空失敗"];
//            }
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            NSLog(@"%@", error);
//            [SVProgressHUD showSuccessWithStatus:@"數據清空失敗"];
//        }];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:true completion:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return Height_Real(10);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenW, Height_Real(10)))];
    return v;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return Height_Real(45);
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = nil;
        _tableView.tableFooterView = nil;
    }
    return _tableView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
